package com.sms;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.huawei.insa2.comm.smgp.SMGPConstant;
import com.huawei.insa2.comm.smgp.message.SMGPMessage;
import com.huawei.insa2.util.TypeConvert;

public class SMGPSubmit30Message extends SMGPMessage{
	private StringBuffer strBuf;
	/**
	 * (int msgType, int needReport, int priority, String serviceId, String feeType, String feeCode, 
	 * String fixedFee, int msgFormat, Date validTime, Date atTime, String srcTermId, String chargeTermId, 
	 * String[] destTermId, String msgContent, String reserve)
	 * 
	 * 
	 * (int validTime, int atTime, int msgFormat,
			String feeCode, String fixedFee, String msgContent, String reserve,
			int destTermId, Date srcTermId, Date chargeTermId,
			String needReport, String msgType, String[] feeType,
			String serviceId, String priority)
	 * */
	public SMGPSubmit30Message(int msgType, int needReport, int priority, String serviceId, String feeType, String feeCode, 
			 String fixedFee, int msgFormat, Date validTime, Date atTime, String srcTermId, String chargeTermId, 
			 String[] destTermId, String msgContent, String reserve) throws IllegalArgumentException {
		/*super(validTime, atTime, msgFormat, feeCode, fixedFee, msgContent, reserve,
				destTermId, srcTermId, chargeTermId, needReport, msgType, feeType,
				serviceId, priority);*/
		
		 if ((msgType < 0) || (msgType > 255))
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":MsgType ").append(SMGPConstant.INT_SCOPE_ERROR))));
		    }

		    if ((needReport < 0) || (needReport > 255))
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":NeedReport ").append(SMGPConstant.INT_SCOPE_ERROR))));
		    }

		    if ((priority < 0) || (priority > 9))
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":").append(SMGPConstant.PRIORITY_ERROR))));
		    }

		    if (serviceId == null)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":ServiceID ").append(SMGPConstant.STRING_NULL))));
		    }

		    if (serviceId.length() > 10)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":ServiceID ").append(SMGPConstant.STRING_LENGTH_GREAT).append("10"))));
		    }

		    if (feeType == null)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":FeeType ").append(SMGPConstant.STRING_NULL))));
		    }

		    if (feeType.length() > 2)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":FeeType ").append(SMGPConstant.STRING_LENGTH_GREAT).append("2"))));
		    }

		    if (feeCode == null)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":FeeCode ").append(SMGPConstant.STRING_NULL))));
		    }

		    if (feeCode.length() > 6)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":FeeCode ").append(SMGPConstant.STRING_LENGTH_GREAT).append("6"))));
		    }

		    if (fixedFee == null)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":FixedFee ").append(SMGPConstant.STRING_NULL))));
		    }

		    if (fixedFee.length() > 6)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":FixedFee ").append(SMGPConstant.STRING_LENGTH_GREAT).append("6"))));
		    }

		    if ((msgFormat < 0) || (msgFormat > 255))
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":MsgFormat ").append(SMGPConstant.INT_SCOPE_ERROR))));
		    }

		    if (srcTermId == null)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":SrcTermID ").append(SMGPConstant.STRING_NULL))));
		    }

		    if (srcTermId.length() > 21)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":SrcTermID ").append(SMGPConstant.STRING_LENGTH_GREAT).append("21"))));
		    }

		    if (chargeTermId == null)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":ChargeTermID ").append(SMGPConstant.STRING_NULL))));
		    }

		    if (chargeTermId.length() > 21)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":ChargeTermID ").append(SMGPConstant.STRING_LENGTH_GREAT).append("21"))));
		    }

		    if (destTermId == null)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":DestTermID ").append(SMGPConstant.STRING_NULL))));
		    }

		    if (destTermId.length > 100)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":").append(SMGPConstant.DESTTERMID_ERROR))));
		    }

		    for (int i = 0; i < destTermId.length; i++)
		    {
		      if (destTermId[i].length() <= 21)
		        continue;
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":one DestTermID ").append(SMGPConstant.STRING_LENGTH_GREAT).append("21"))));
		    }

		    if (msgContent == null)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":MsgContent ").append(SMGPConstant.STRING_NULL))));
		    }

		    if (msgContent.getBytes().length > 252)
		    {
		      throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":MsgContent ").append(SMGPConstant.STRING_LENGTH_GREAT).append("252"))));
		    }

		    if (reserve != null)
		    {
		      if (reserve.length() > 8)
		      {
		        throw new IllegalArgumentException(String.valueOf(String.valueOf(new StringBuffer(String.valueOf(String.valueOf(SMGPConstant.SUBMIT_INPUT_ERROR))).append(":reserve ").append(SMGPConstant.STRING_LENGTH_GREAT).append("8"))));
		      }

		    }

		    int len = 126 + 21 * destTermId.length + msgContent.getBytes().length;

		    this.buf = new byte[len];

		    TypeConvert.int2byte(len, this.buf, 0);
		    TypeConvert.int2byte(2, this.buf, 4);

		    this.buf[12] = (byte)msgType;
		    this.buf[13] = (byte)needReport;
		    this.buf[14] = (byte)priority;
		    System.arraycopy(serviceId.getBytes(), 0, this.buf, 15, serviceId.length());
		    System.arraycopy(feeType.getBytes(), 0, this.buf, 25, feeType.length());
		    System.arraycopy(feeCode.getBytes(), 0, this.buf, 27, feeCode.length());
		    System.arraycopy(fixedFee.getBytes(), 0, this.buf, 33, fixedFee.length());
		    this.buf[39] = (byte)msgFormat;

		    SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");

		    if (validTime != null) {
		      String tmpTime = String.valueOf(String.valueOf(dateFormat.format(validTime))).concat("032+");
		      System.arraycopy(tmpTime.getBytes(), 0, this.buf, 40, 16);
		    }

		    if (atTime != null) {
		      String tmpTime = String.valueOf(String.valueOf(dateFormat.format(atTime))).concat("032+");
		      System.arraycopy(tmpTime.getBytes(), 0, this.buf, 57, 16);
		    }

		    System.arraycopy(srcTermId.getBytes(), 0, this.buf, 74, srcTermId.length());

		    System.arraycopy(chargeTermId.getBytes(), 0, this.buf, 95, chargeTermId.length());

		    this.buf[116] = (byte)destTermId.length;

		    int i = 0;
		    for (i = 0; i < destTermId.length; i++)
		    {
		      System.arraycopy(destTermId[i].getBytes(), 0, this.buf, 117 + i * 21, destTermId[i].length());
		    }
		    int loc = 117 + i * 21;

		    this.buf[loc] = (byte)msgContent.getBytes().length;

		    System.arraycopy(msgContent.getBytes(), 0, this.buf, loc + 1, msgContent.getBytes().length);

		    loc = loc + 1 + msgContent.getBytes().length;
		    if (reserve != null)
		    {
		      System.arraycopy(reserve.getBytes(), 0, this.buf, loc, reserve.length());
		    }

		    this.strBuf = new StringBuffer(600);
		    this.strBuf.append(",MsgType=".concat(String.valueOf(String.valueOf(msgType))));
		    this.strBuf.append(",NeedReport=".concat(String.valueOf(String.valueOf(needReport))));
		    this.strBuf.append(",Priority=".concat(String.valueOf(String.valueOf(priority))));
		    this.strBuf.append(",ServiceID=".concat(String.valueOf(String.valueOf(serviceId))));
		    this.strBuf.append(",FeeType=".concat(String.valueOf(String.valueOf(feeType))));
		    this.strBuf.append(",FeeCode=".concat(String.valueOf(String.valueOf(feeCode))));
		    this.strBuf.append(",FixedFee=".concat(String.valueOf(String.valueOf(fixedFee))));
		    this.strBuf.append(",MsgFormat=".concat(String.valueOf(String.valueOf(msgFormat))));
		    if (validTime != null) {
		      this.strBuf.append(",ValidTime=".concat(String.valueOf(String.valueOf(dateFormat.format(validTime)))));
		    }
		    else
		    {
		      this.strBuf.append(",ValidTime=null");
		    }
		    if (atTime != null)
		    {
		      this.strBuf.append(",AtTime=".concat(String.valueOf(String.valueOf(dateFormat.format(atTime)))));
		    }
		    else
		    {
		      this.strBuf.append(",at_Time=null");
		    }
		    this.strBuf.append(",SrcTermID=".concat(String.valueOf(String.valueOf(srcTermId))));
		    this.strBuf.append(",ChargeTermID=".concat(String.valueOf(String.valueOf(chargeTermId))));
		    this.strBuf.append(",DestTermIDCount=".concat(String.valueOf(String.valueOf(destTermId.length))));
		    for (int t = 0; t < destTermId.length; t++)
		    {
		      this.strBuf.append(String.valueOf(String.valueOf(new StringBuffer(",DestTermID[").append(t).append("]=").append(destTermId[t]))));
		    }
		    this.strBuf.append(",MsgLength=".concat(String.valueOf(String.valueOf(msgContent.getBytes().length))));
		    this.strBuf.append(",MsgContent=".concat(String.valueOf(String.valueOf(msgContent))));
		    this.strBuf.append(",Reserve=".concat(String.valueOf(String.valueOf(reserve))));
		
	}
	public String toString()
	  {
	    StringBuffer outBuf = new StringBuffer(600);
	    outBuf.append("SMGPSubmitMessage: ");
	    outBuf.append("PacketLength=".concat(String.valueOf(String.valueOf(this.buf.length))));
	    outBuf.append(",RequestID=2");
	    outBuf.append(",SequenceID=".concat(String.valueOf(String.valueOf(getSequenceId()))));
	    if (this.strBuf != null)
	    {
	      outBuf.append(this.strBuf.toString());
	    }
	    return outBuf.toString();
	  }

	  public int getRequestId()
	  {
	    return 2;
	  }
	
}
